package com.tianqu.tms.entity;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.tianqu.tms.core.base.TmsEntity;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.dao.IntegralRegisterDao;
import com.tianqu.tms.dao.TmsDao;
import com.tianqu.tms.dto.AttnDto;
import com.tianqu.tms.dto.IntegralRegisterDto;
import com.tianqu.tms.dto.MemberDto;

/**
 * 积分管理
 * 
 * @author Administrator
 * 
 */
public class IntegralRegister extends TmsEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3210600235906210884L;
	private int cssFlag;
	private int id;
	private String memberId;// 会员ID
	private String serial;// 车辆编号
	private int memberType;
	private String licensePlate;// 车牌号码
	private int transferPrice;// 过户费
	private int insertUserId;
	private Timestamp insertTime;
	private int priceAll;
	private int transferPriceAll;
	private int transferTime;
	private int chargeType;//	收费类型	
	private int Integral;//	处理积分
	private int plusMinus;//	积分增减
	private int usedIntegral;
	private int updateUserId;
	private Timestamp updateTime;
	private String remark;

	public static IntegralRegister getInstance() {
		IntegralRegister marketCarGateInfo = new IntegralRegister();
		marketCarGateInfo.setExisting(false);
		return marketCarGateInfo;
	}
	
	public List<IntegralRegisterDto> findIntegralByMemId(String memId,int memberType,int chargeType) throws TmsDaoException{
		List<IntegralRegisterDto> integralRegisterDtoList = new ArrayList<IntegralRegisterDto>();
		IntegralRegisterDao uDao = (IntegralRegisterDao) TmsDao.getInstance(TmsDao.TMS_INTEGRAL_REGISTER);
		integralRegisterDtoList = uDao.findIntegralByMemId(memId,memberType,chargeType);
		return integralRegisterDtoList;
	}
	public List<IntegralRegisterDto> findIntegralListByMemId(String memId,int memberType,int chargeType,int plusMinus) throws TmsDaoException{
		List<IntegralRegisterDto> integralRegisterDtoList = new ArrayList<IntegralRegisterDto>();
		IntegralRegisterDao uDao = (IntegralRegisterDao) TmsDao.getInstance(TmsDao.TMS_INTEGRAL_REGISTER);
		integralRegisterDtoList = uDao.findIntegralListByMemId(memId,memberType,chargeType,plusMinus);
		return integralRegisterDtoList;
	}

	/**
	 * 根据载入memId载入积分
	 * @param memId
	 * @return
	 * @throws TmsDaoException 
	 */
	public IntegralRegister findIntegralByMemId(String memId) throws TmsDaoException{
		IntegralRegister entity = IntegralRegister.getInstance();
		IntegralRegisterDao uDao = (IntegralRegisterDao) TmsDao.getInstance(TmsDao.TMS_INTEGRAL_REGISTER);
		List<IntegralRegisterDto> integralRegisterDtoList = new ArrayList<IntegralRegisterDto>();
		integralRegisterDtoList = uDao.findIntegralByMemId(memId);
		int transferPriceAll = 0;
		int usedIntegralAll = 0;
		for(IntegralRegisterDto dto : integralRegisterDtoList){
			transferPriceAll = transferPriceAll + dto.getTransferPrice();
			if(dto.getPlusMinus() == 2) usedIntegralAll=usedIntegralAll+dto.getIntegral();
		}
		entity.setTransferTime(integralRegisterDtoList.size());
		entity.setTransferPriceAll(transferPriceAll);
		entity.setUsedIntegral(usedIntegralAll);
		return entity;
	}
	/**
	 * 插入
	 * @return
	 * @throws TmsDaoException 
	 */
	public void insertRegisterInfo(IntegralRegister entity) throws TmsDaoException
	{
		IntegralRegisterDao uDao = (IntegralRegisterDao) TmsDao.getInstance(TmsDao.TMS_INTEGRAL_REGISTER);
		IntegralRegisterDto dto = new IntegralRegisterDto();
		dto.setMemberId(entity.getMemberId());
		dto.setMemberType(entity.getMemberType());
		dto.setSerial(entity.getSerial());
		dto.setLicensePlate(entity.getLicensePlate());
		dto.setTransferPrice(entity.getTransferPrice());
		dto.setInsertUserId(entity.getInsertUserId());
		dto.setPlusMinus(entity.getPlusMinus());
		dto.setChargeType(entity.getChargeType());
		dto.setIntegral(entity.getIntegral());	
		dto.setRemark(entity.getRemark());
		uDao.insertRegisterInfo(dto);
	}
	/**
	 * 根据memberID查询所有积分和
	 * @param memberId
	 * @return
	 * @throws TmsDaoException
	 */
	public int findAllTransferPriceByMemId(String memberId) throws TmsDaoException
	{
		IntegralRegisterDao uDao = (IntegralRegisterDao) TmsDao.getInstance(TmsDao.TMS_INTEGRAL_REGISTER);
		IntegralRegisterDto dto = new IntegralRegisterDto();
		dto = uDao.findAllTransferPriceByMemId(memberId);
		if(dto!=null)
		{
			return dto.getTransferPrice();
		}
		return 0;
	}
	/**
	 * 根据流水号查询积分卡信息
	 * @return
	 */
	public IntegralRegister getIntegralRegisterInfoBySerial(String serial,int chargeType,int plusMinus) throws TmsDaoException
	{
		IntegralRegisterDao uDao = (IntegralRegisterDao) TmsDao.getInstance(TmsDao.TMS_INTEGRAL_REGISTER);
		IntegralRegisterDto dto = new IntegralRegisterDto();
		dto = uDao.getIntegralRegisterInfoBySerial(serial,chargeType,plusMinus);
		IntegralRegister entity = null;
		if(dto!=null)
		{
			entity = new IntegralRegister();
			entity.setMemberId(dto.getMemberId());
			entity.setMemberType(dto.getMemberType());
			entity.setSerial(dto.getSerial());
			entity.setLicensePlate(dto.getLicensePlate());
			entity.setChargeType(dto.getChargeType());
			entity.setTransferPrice(dto.getTransferPrice());
			entity.setIntegral(dto.getIntegral());
			entity.setPlusMinus(dto.getPlusMinus());
		}
		return entity;	
	}
	public void updateIntegralRegisterInfo(IntegralRegister entity)throws TmsDaoException
	{
		IntegralRegisterDao uDao = (IntegralRegisterDao) TmsDao.getInstance(TmsDao.TMS_INTEGRAL_REGISTER);
		IntegralRegisterDto dto = new IntegralRegisterDto();
		dto.setTransferPrice(entity.getTransferPrice());
		dto.setSerial(entity.getSerial());
		dto.setChargeType(entity.getChargeType());
		dto.setIntegral(entity.getIntegral());
		dto.setUpdateUserId(entity.getUpdateUserId());
		uDao.updateIntegralRegisterInfo(dto);
	}
	/**
	 * 根据流水号查询积分卡信息是否存在
	 * @return
	 */
	public boolean getIntegralRegisterBySerial(String serial) throws TmsDaoException
	{
		boolean result = false;
		IntegralRegisterDao uDao = (IntegralRegisterDao) TmsDao.getInstance(TmsDao.TMS_INTEGRAL_REGISTER);
		result = uDao.getIntegralRegisterBySerial(serial);
		return result;	
	}
	public void deleteIntegralRegisterInfo(String serial,int chargeType)throws TmsDaoException
	{
		IntegralRegisterDao uDao = (IntegralRegisterDao) TmsDao.getInstance(TmsDao.TMS_INTEGRAL_REGISTER);
		uDao.deleteIntegralRegisterInfo(serial,chargeType);
	}
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getMemberType() {
		return memberType;
	}

	public void setMemberType(int memberType) {
		this.memberType = memberType;
	}

	public String getSerial() {
		return serial;
	}

	public void setSerial(String serial) {
		this.serial = serial;
	}

	public String getLicensePlate() {
		return licensePlate;
	}

	public void setLicensePlate(String licensePlate) {
		this.licensePlate = licensePlate;
	}

	public int getTransferPrice() {
		return transferPrice;
	}

	public void setTransferPrice(int transferPrice) {
		this.transferPrice = transferPrice;
	}

	public int getPriceAll() {
		return priceAll;
	}

	public void setPriceAll(int priceAll) {
		this.priceAll = priceAll;
	}

	public int getTransferPriceAll() {
		return transferPriceAll;
	}

	public void setTransferPriceAll(int transferPriceAll) {
		this.transferPriceAll = transferPriceAll;
	}

	public String getMemberId() {
		return memberId;
	}

	public void setMemberId(String memberId) {
		this.memberId = memberId;
	}

	public int getInsertUserId() {
		return insertUserId;
	}

	public void setInsertUserId(int insertUserId) {
		this.insertUserId = insertUserId;
	}

	public Timestamp getInsertTime() {
		return insertTime;
	}

	public void setInsertTime(Timestamp insertTime) {
		this.insertTime = insertTime;
	}

	public int getTransferTime() {
		return transferTime;
	}

	public void setTransferTime(int transferTime) {
		this.transferTime = transferTime;
	}

	public int getChargeType() {
		return chargeType;
	}

	public void setChargeType(int chargeType) {
		this.chargeType = chargeType;
	}

	public int getIntegral() {
		return Integral;
	}

	public void setIntegral(int integral) {
		Integral = integral;
	}

	public int getPlusMinus() {
		return plusMinus;
	}

	public void setPlusMinus(int plusMinus) {
		this.plusMinus = plusMinus;
	}

	public int getUpdateUserId() {
		return updateUserId;
	}

	public void setUpdateUserId(int updateUserId) {
		this.updateUserId = updateUserId;
	}

	public Timestamp getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Timestamp updateTime) {
		this.updateTime = updateTime;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	
	public int getUsedIntegral() {
		return usedIntegral;
	}

	public void setUsedIntegral(int usedIntegral) {
		this.usedIntegral = usedIntegral;
	}

	@Override
	public void save() throws TmsDaoException, TmsException {
		// TODO Auto-generated method stub

	}
	
	private String dateFrom ;
	private String dateTo;
	private int count;//总数量
	private int no;//序号
	private String attnName;//交费人
	private String shortName;//公司名称
	private String chargeTypeStr; // 业务处理类型
	private int totalTransferPrice;//页面头总金额
	private int sumTransferPrice; // 页面尾总金额
	private String insertTimeStr;
	private List<IntegralRegister> info;
	
	/**
	 * 一、预收款汇总表  
	 * dateFrom yyyyMMdd
	 * dateTo yyyyMMdd
	 */
	public IntegralRegister findIntegralRegisterInfo(String memId, String dateFrom, String dateTo) throws TmsDaoException{
		Member instance = Member.getInstance();
		Map<String, MemberDto> memberMap = instance.findAllMember();
		Attn attnManager = Attn.getInstance();
		Map<String, AttnDto> attnMap = attnManager.findAllAttn();
		IntegralRegisterDao registerDao = (IntegralRegisterDao) TmsDao.getInstance(TmsDao.TMS_INTEGRAL_REGISTER);
		int i = 1;
		int sumTransferPrice = 0;
		List<IntegralRegister> list = new ArrayList<IntegralRegister>();
		List<IntegralRegisterDto> registerList = registerDao.findIntegral(memId, dateFrom, dateTo);
		for (IntegralRegisterDto integralRegisterDto : registerList) {
			IntegralRegister register = new IntegralRegister();
			register.setNo(i++);
			register.setMemberId(integralRegisterDto.getMemberId());
			AttnDto attn = attnMap.get(integralRegisterDto.getMemberId());
			if(attn != null)  {
				register.setAttnName(attn.getAttnName());
				MemberDto member = memberMap.get(attn.getMemberId());
				if(member != null) register.setShortName(member.getShortName());
			}
			String time = DateUtil.getTimeByTimeStamp(integralRegisterDto.getInsertTime());
			register.setInsertTimeStr(time==null?"--":time);
			register.setChargeTypeStr("过户服务费充值");
			register.setTransferPrice(integralRegisterDto.getTransferPrice());
			if(i%2 == 0) register.setCssFlag(1);
			else register.setCssFlag(2);
			
			sumTransferPrice = sumTransferPrice + integralRegisterDto.getTransferPrice();
			list.add(register);
		}
		
		//
		IntegralRegister info = new IntegralRegister();
		info.setInfo(list);
		info.setSumTransferPrice(sumTransferPrice);
		info.setTotalTransferPrice(sumTransferPrice);
		info.setDateFrom(dateFrom);
		info.setDateTo(dateTo);
		info.setCount(registerList.size());
		return info;
	}

	/**
	 * 
	 */
	public String getDateFrom() {
		return dateFrom;
	}

	public void setDateFrom(String dateFrom) {
		this.dateFrom = dateFrom;
	}

	public String getDateTo() {
		return dateTo;
	}

	public void setDateTo(String dateTo) {
		this.dateTo = dateTo;
	}

	public String getAttnName() {
		return attnName;
	}

	public void setAttnName(String attnName) {
		this.attnName = attnName;
	}

	public String getShortName() {
		return shortName;
	}

	public void setShortName(String shortName) {
		this.shortName = shortName;
	}

	public String getChargeTypeStr() {
		return chargeTypeStr;
	}

	public void setChargeTypeStr(String chargeTypeStr) {
		this.chargeTypeStr = chargeTypeStr;
	}

	public List<IntegralRegister> getInfo() {
		return info;
	}

	public void setInfo(List<IntegralRegister> info) {
		this.info = info;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public int getTotalTransferPrice() {
		return totalTransferPrice;
	}

	public void setTotalTransferPrice(int totalTransferPrice) {
		this.totalTransferPrice = totalTransferPrice;
	}

	public int getNo() {
		return no;
	}

	public void setNo(int no) {
		this.no = no;
	}

	public int getSumTransferPrice() {
		return sumTransferPrice;
	}

	public void setSumTransferPrice(int sumTransferPrice) {
		this.sumTransferPrice = sumTransferPrice;
	}

	public int getCssFlag() {
		return cssFlag;
	}

	public void setCssFlag(int cssFlag) {
		this.cssFlag = cssFlag;
	}

	public String getInsertTimeStr() {
		return insertTimeStr;
	}

	public void setInsertTimeStr(String insertTimeStr) {
		this.insertTimeStr = insertTimeStr;
	}
	
	
}
